15. Delete Pet From EditorActivity
Deleting Pets from EditorActivity
Question:
Our last big hurdle is to implement the ability to delete pets. This is in two places in the app - the first is on the EditorActivity page’s menu, when you’re updating a pet, the second is in the CatalogActivity’s menu. In the catalog activity, the delete option deletes all pets, which is available for when you’re testing the database.
Let’s start with the EditorActivity delete method. We already added the code so that it only appears on the “edit pet mode”. Now what I want to have happen when we select Delete pet, is for this dialog to appear. Pressing Delete should show the toast “Pet deleted” and delete the pet from our database.
Let’s copy over this Gist. It includes some new strings for String.xml. It also includes code to generate the dialog, in a method called “showDeleteConfirmationDialog” and an empty “deletePet” method.
The dialog has two onclick listeners, one for the button that says “delete” shown here, and the other for the button that says “cancel” shown here. The delete dialog calls the empty “deletePet” method, which is where you need to add the code to actually delete the current pet.
Your Task:
First, find the correct place in the code so that when you select Delete Pet from the drop down menu, it opens the DeleteConfirmationDialog
Next, fill out the deletePet method so that the current pet is deleted. You should only delete the pet if it’s in “edit mode”. You should also show a toast which states whether or not the pet was successfully deleted, much in the same way we show a toast about whether the pet was successfully updated.
Start Quiz:
Solution:
After copying over the gist, we first make sure the showDeleteConfirmationDialog actually is triggered by calling it when the delete menu button is pressed. I do that by going to the onOptionsItemSelected and under action.delete, adding the call to showDeleteConfirmationDialog.
// Respond to a click on the "Delete" menu option
case R.id.action_delete:
// Pop up confirmation dialog for deletion
showDeleteConfirmationDialog();
showDeleteConfirmationDialog will create a dialog that calls deletePet when the delete button is pressed. First I make sure that we only proceed with deletion for an existing pet. There’s no point in performing a deletion in the database for a new pet that doesn’t exist there yet. So I check for whether there is an existing pet or not (mCurrentPetUri equals null or not). Then use the content resolver and the current pet uri to delete the pet.
/**
* Perform the deletion of the pet in the database.
*/
private void deletePet() {
// Only perform the delete if this is an existing pet.
if (mCurrentPetUri != null) {
// Call the ContentResolver to delete the pet at the given content URI.
// Pass in null for the selection and selection args because the mCurrentPetUri
// content URI already identifies the pet that we want.
int rowsDeleted = getContentResolver().delete(mCurrentPetUri, null, null);
The delete method, like update, returns the number of rows deleted. I can see if the delete was successful by checking whether 0 rows were deleted. If zero rows were deleted, then the delete was not successful and I’ll show a toast that says “ Error with deleting pet”. Otherwise the operation was successful, and I pop up a toast that says “Pet deleted”.
// Show a toast message depending on whether or not the delete was successful.
if (rowsDeleted == 0) {
// If no rows were deleted, then there was an error with the delete.
Toast.makeText(this, getString(R.string.editor_delete_pet_failed),
Toast.LENGTH_SHORT).show();
} else {
// Otherwise, the delete was successful and we can display a toast.
Toast.makeText(this, getString(R.string.editor_delete_pet_successful),
Toast.LENGTH_SHORT).show();
}
Once the operation is done, then the activity can be closed by calling the finish() method.
// Close the activity
finish();
With all that, we should be able to successfully delete pets from the EditorActivity!